package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.po.LycheeVariety;
import com.example.error.exceptions.LycheeIsNullException;
import com.example.service.LycheeVarietyService;
import com.example.mapper.LycheeVarietyMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author adim
* @description 针对表【lychee_variety(荔枝品种信息表，存储品种特性与文化内容)】的数据库操作Service实现
* @createDate 2025-06-10 17:20:32
*/
@Service
@RequiredArgsConstructor
public class LycheeVarietyServiceImpl extends ServiceImpl<LycheeVarietyMapper, LycheeVariety>
    implements LycheeVarietyService{

    private final RedisTemplate redisTemplate;
    private final  LycheeVarietyMapper lycheeVarietyMapper;

    /**W
     * 获取所有荔枝品种
     * @return
     */
    @Override
    public List<LycheeVariety> listLychee() {
        //读取redis数据库
        List<LycheeVariety> list = (List<LycheeVariety>) redisTemplate.opsForValue().get("lychee_variety");
        if (list != null){
            return list;
        }
        //redis数据库为空，从数据库查询
        list = lycheeVarietyMapper.selectList(null);


        if (Objects.isNull(list)){
            throw new LycheeIsNullException("荔枝品种为空");
        }
        //存入redis数据库
        redisTemplate.opsForValue().set("lychee_variety",list);
        return list;
    }

    /**
     * 根据id查询荔枝品种
     */
    @Override
    public LycheeVariety getLycheeById(Integer id) {

        return lycheeVarietyMapper.selectById(id);
    }
}




